import 'package:get/get.dart';

import '../index.dart';

class UserService extends GetxService {
  static UserService get to => Get.find();
  final _storage = Storage();
  final user = Rxn<UserModel>();

  // 获取当前用户
  UserModel? get_now_user() {
    final username = _storage.get_string(Constants.current_user);
    if (username?.isNotEmpty != true) return null;

    final user_json = _storage.get_json('user_$username');
    if (user_json == null) return null;

    try {
      user.value = UserModel.fromJson(user_json);
      return user.value;
    } catch (e) {
      return null;
    }
  }

  // 将默认用户数据存入缓存
  void init_user_data() {
    final is_init = _storage.get_bool(Constants.is_init_user_data);
    if (is_init) return;

    _storage.set_bool(Constants.is_init_user_data, true);

    for (var user in user_data.values) {
      final list = _storage.get_list(Constants.username_list);
      if (!list.contains(user.name)) {
        list.add(user.name!);
        _storage.set_list(Constants.username_list, list);
      }
    }
  }

  // 根据用户ID获取用户数据
  UserModel? get_user_data_by_id(String user_id) {
    final list = _storage.get_list(Constants.username_list);
    if (list.isEmpty) return null;

    UserModel? found_user;
    list.forEach((username) {
      final userJson = _storage.get_json('user_$username');
      if (userJson != null) {
        final user = UserModel.fromJson(userJson);
        if (user.id == user_id) {
          found_user = user;
        }
      }
    });
    return found_user;
  }

  // 根据用户名获取用户数据
  UserModel? get_user_data_by_username(String username) {
    final user_json = _storage.get_json('user_$username');
    if (user_json == null) {
      return null;
    }
    return UserModel.fromJson(user_json);
  }

  // 设置用户
  bool set_user(UserModel value) {
    try {
      user.value = value;
      _storage.set_json('user_${value.name}', value.toJson());
      if (value.id != null) {
        _storage.set_string(Constants.current_user, value.name!);
      }
      _storage.set_bool(Constants.login_status, true);
      return true;
    } catch (e) {
      return false;
    }
  }

  // 登录
  bool login_user(String username, String password) {
    // 检查缓存的用户数据
    final user_json = _storage.get_json('user_$username');
    if (user_json == null) return false;

    try {
      final stored_user = UserModel.fromJson(user_json);
      if (stored_user.password != password) return false;

      set_user(stored_user);
      return true;
    } catch (e) {
      return false;
    }
  }

  // 注册
  Future<bool> register_user(UserModel user) async {
    if (user.name == null || user.name!.isEmpty) return false;
    //将name存入sp
    final list = _storage.get_list(Constants.username_list);
    if (list.contains(user.name)) return false;
    list.add(user.name!);
    _storage.set_list(Constants.username_list, list);

    // 将user存入sp
    await _storage.set_json('user_${user.name}', user.toJson());

    return true;
  }

  // 删除用户
  void delete_user(String username) {
    _storage.remove_data('user_$username');
    final list = _storage.get_list(Constants.username_list);
    list.remove(username);
    _storage.set_list(Constants.username_list, list);
  }

  // 清除用户
  void clear_user() {
    user.value = null;
    _storage.remove_data(Constants.current_user);
    _storage.set_bool(Constants.login_status, false);
  }

  // 登出
  void logout_user() {
    clear_user();
  }
}
